﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;

namespace Entity.Core
{
    /// <summary>
    /// 生成实体代码
    /// </summary>
    public class CreateEntity
    {
        /// <summary>
        /// 生成实体代码
        /// </summary>
        /// <param name="reader">DataReader</param>
        /// <returns>代码字符串</returns>
        /// <param name="tableName">表名，用于生成类名</param>
        public static string CreateCode(DbDataReader reader, string tableName)
        {
            DataTable schemaTable = reader.GetSchemaTable();
            StringBuilder code = new StringBuilder(CommonReference.Reference);
            code.AppendLine();
            code.AppendFormat("public class {0}", tableName);
            code.AppendLine();
            code.AppendLine("{");
            foreach (DataRow row in schemaTable.Rows)
            {
                //生成每一个属性
                code.AppendLine(CreateProperty.CreateCode(row));
                code.AppendLine();
            }
            code.AppendLine("}");
            reader.Close();
            return code.ToString(); ;
        }
    }
}
